#!/bin/bash
export PATH=/home/postgres/psql14/bin:$PATH
export PGDATABASE=postgres
export PGHOST=127.0.0.1
export PGUSER=postgres
export PGPORT=15433
export PGPASSWORD=123456
slaveDataDir=/home/postgres/slavedata/pgdata
slavepushDataDir=/home/postgres/slavepushdata/pgdata

sed -i 's/^he3share/#he3share/g' $slaveDataDir/postgresql.auto.conf
sed -i 's/^he3share/#he3share/g' $slaveDataDir/postgresql.conf
sed -i 's/^hot_standby/#hot_standby/g' $slaveDataDir/postgresql.conf
sed -i 's/^primary_conninfo/#primary_conninfo/g' $slaveDataDir/postgresql.auto.conf
sed -i 's/^primary_conninfo/#primary_conninfo/g' $slaveDataDir/postgresql.conf

echo -e "he3share = on" >> $slaveDataDir/postgresql.conf
echo -e "hot_standby=off" >> $slaveDataDir/postgresql.conf

sed -i 's/^mpush/#mpush/g' $slavepushDataDir/postgresql.conf
sed -i 's/^mpush/#mpush/g' $slavepushDataDir/postgresql.auto.conf

echo -e "mpush=on" >> $slavepushDataDir/postgresql.conf

psql -c 'SELECT pg_promote(true, 30)'
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB slave instance promote failed!"
    exit 1
fi

pg_ctl -D $slaveDataDir reload
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB slave instance reload failed!"
    exit 1
fi

pg_ctl -D $slavepushDataDir reload
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB push instance reload failed!"
    exit 1
fi